#实现登录、日志（5个）
#文件上传
#定义文件参数：'message_img'键来自抓包时的name对应的值，
#值是通过数组的形式传值，第一个参数是抓包时的'filename'对应的值，
#                      第二个参数是通过open打开一个文件，且是二进制读的形式，
#                      第三个参数是抓包时的文件类型
import requests

from APIs.baseAPI import BaseAPI
from APIs.loginAPI import LoginAPI
from common.config import ip


class LogAPI(BaseAPI):
    def upload(self):
        furl=r"D:\lius\pythonCode\APIAuto\datas\47586e5d4a4e39dbeb803ca09a8aece.png"
        fileD={"file":("47586e5d4a4e39dbeb803ca09a8aece.png",
                       open(furl,"rb"),
                       "image/jpeg"
        )}
        r6=requests.post(f"{ip}/file/upload",headers=self.myheader,
                         files=fileD)
        print(r6)
        return r6.json()
#新增日志

    def add(self,data):

        # data={
        #   "categoryId": "1",
        #   "content": "111",
        #   "tomorrow": "222",
        #   "question": "333",
        #   "batchId": "c8db676d8e66656af17b9c9332357b25",
        #   "sendUserIds": "",
        #   "sendDeptIds": "",
        #   "customerIds": "",
        #   "contactsIds": "",
        #   "businessIds": "",
        #   "contractIds": ""
        # }
        r1=requests.post(f"{ip}/OaLog/addOrUpdate",headers=self.myheader,json=data)
        print(r1)
        return r1.json()
#修改日志

    def update(self,data):
        # data={
        #   "logId": 145,
        #   "categoryId": "1",
        #   "content": "111",
        #   "tomorrow": "111",
        #   "question": "111",
        #   "batchId": "46144d8a2b3f162b457d4f589071cd45",
        #   "sendUserIds": "",
        #   "sendDeptIds": "",
        #   "customerIds": "",
        #   "contactsIds": "",
        #   "businessIds": "",
        #   "contractIds": ""
        # }
        r2=requests.post(f"{ip}/OaLog/addOrUpdate",headers=self.myheader,json=data)
        print(r2)
        return r2.json()
#获取日志

    def query(self,data):
        # data={
        #   "page": 1,
        #   "limit": 5
        # }
        r3=requests.post(f"{ip}/OaLog/queryList",headers=self.myheader,json=data)
        print(r3)
        return r3.json()
# 删除日志

    def delete(self):
        # 获取日志
        code={
          "page": 1,
          "limit": 5
        }
        r3=requests.post(f"{ip}/OaLog/queryList",headers=self.myheader,json=code)
        id=r3.json()["data"]["list"][0]["logId"]
        print(id)
        sLogid={"logId":id}
        r4=requests.post(f"{ip}/OaLog/deleteById",headers=self.myheader,data=sLogid)
        print(r4)
        return r4.json()
if __name__ == '__main__':
    f=LoginAPI()
    s=f.getToken()
    f1=LogAPI(s)
    print(f1.add(data={
          "categoryId": "1",
          "content": "111",
          "tomorrow": "222",
          "question": "333",
          "batchId": "c8db676d8e66656af17b9c9332357b25",
          "sendUserIds": "",
          "sendDeptIds": "",
          "customerIds": "",
          "contactsIds": "",
          "businessIds": "",
          "contractIds": ""
        }))
    print(f1.update(data={
          "logId": 145,
          "categoryId": "1",
          "content": "111",
          "tomorrow": "111",
          "question": "111",
          "batchId": "46144d8a2b3f162b457d4f589071cd45",
          "sendUserIds": "",
          "sendDeptIds": "",
          "customerIds": "",
          "contactsIds": "",
          "businessIds": "",
          "contractIds": ""
        }))
    print(f1.query(data={
          "page": 1,
          "limit": 5
        }))
    print(f1.delete(data={
          "page": 1,
          "limit": 5
        }))
